<?xml version="1.0"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Debugging Python(Twisted) with Emacs</title>
</head>
<body>
<h1>Debugging Python(Twisted) with Emacs</h1>

<img src="http://yellow5.com/pokey/archive/pokey411_3.gif"/>
<span class="footnote">POKEY THE PENGUIN IS COPYRIGHT &copy; 1998-2002
THE AUTHORS</span>

<ul>
  <li>Open up your project files. sometimes emacs can't find them if you
   don't have them open before-hand.</li>

  <li>Make sure you have a program called <code class="shell">pdb</code> somewhere
  in your PATH, with the following contents:

  <pre class="shell">#!/bin/sh
exec python2.3 /usr/lib/python2.3/pdb.py $1 $2 $3 $4 $5 $6 $7 $8 $9
  </pre></li>
  
  <li>Run <code class="shell">M-x pdb</code> in emacs. If you usually run your
  program as <code class="shell">python foo.py</code>, your command line should be <code class="shell">pdb
  foo.py</code>, for <code class="shell">twistd</code> and <code class="shell">trial</code> just
  add -b to the command line, e.g.: <code class="shell">twistd -b -y  my.tac</code></li>

  <li>While pdb waits for your input, go to a place in your code and hit
   <code class="shell">C-x SPC</code> to insert a break-point. pdb should say something happy.
   Do this in as many points as you wish.</li>

   <li>Go to your pdb buffer and hit <code class="shell">c</code>; this runs as normal until a
   break-point is found.</li>

   <li>Once you get to a breakpoint, use <code class="shell">s</code> to step, <code class="shell">n</code> to run the
   current line without stepping through the functions it calls, <code class="shell">w</code>
   to print out the current stack, <code class="shell">u</code> and <code class="shell">d</code> to go up and down a
   level in the stack, <code class="shell">p foo</code> to print result of expression <code class="shell">foo</code>.</li>

   <li>Recommendations for effective debugging:
   <ul>
     <li>use <code class="shell">p self</code> a lot; just knowing the class where the current code
     is isn't enough most of the time.</li>
     <li>use <code class="shell">w</code> to get your bearings, it'll re-display the current-line/arrow</li>
     <li>after you use <code class="shell">w</code>, use <code class="shell">u</code> and <code class="shell">d</code> and lots more <code class="shell">p self</code> on the
     different stack-levels.</li>
     <li>If you've got a big code-path that you need to grok, keep another
      buffer open and list the code-path there (e.g., I had a
      nasty-evil Deferred recursion, and this helped me tons)</li>
    </ul>
  </li>
</ul>


</body>
</html>

